package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.xymarket.adapter.GoodsAdapter;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.po.Goods;
import com.example.xymarket.httpUtil.GoodsService;
import com.example.xymarket.common.MyHandler;

import java.util.List;
import java.util.Map;

public class ClassifyActivity extends AppCompatActivity {


    private int[] classifies=
            {
                    R.id.category1,R.id.category2,R.id.category3,R.id.category4,
                    R.id.category5,R.id.category6,R.id.category7,R.id.category8
            };
    private List<Map<String, Object>> lists;

    private boolean update=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_classify);
        StatusBar.setStatusBarColor(ClassifyActivity.this,StatusBar.yellow);
        try {
            getClassify(findViewById(R.id.category1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    private GoodsAdapter adapter;
    public void updateListView(List<Goods> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.ll1);
        try {
            adapter = new GoodsAdapter(list,this,R.layout.item_classify,handler2);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }


    @SuppressLint("HandlerLeak")
    final Handler handler2 = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                updateListView((List<Goods>) msg.obj);
            }
            if(msg.what==100) {
                Intent intent=new Intent(getApplicationContext(),GoodsActivity.class);
                Integer id=(Integer) msg.obj;
                intent.putExtra("id", id);
                startActivity(intent);
            }
        }
    };

    public void getClassify(View view) throws Exception {
        Button b1=(Button)view;
        int grey= Color.argb(255,245,245,245);
        int col = Color.argb(0,255,255,255);
        int k=0;
        for(int i=0;i<classifies.length;i++)
        {
            Button b2=(Button)findViewById(classifies[i]);
            if(b2==b1)
            {
                k=i+1;
            }
            b2.setBackgroundColor(grey);
        }
        b1.setBackgroundColor(col);
        MyHandler myHandler=new MyHandler(getApplicationContext(),handler2);
       GoodsService goodsService=new GoodsService(myHandler);
       goodsService.getGoodsByCategory(k);
    }



    /**
     * 页面跳转
     * @param view
     */
    public void toHome(View view)
    {
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }


    public void toShoppingCart(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ShoppingCartActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toMine(View view)
    {
        Intent intent = new Intent(getApplicationContext(), MineActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }


    public void toSearch(View view)
    {
        Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    /**
     * 再按一次退出程序
     * 用时间来计算两次返回之间的时间
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
